package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.constant.PageModel;
import cn.ztuo.bitrade.constant.TransactionType;
import cn.ztuo.bitrade.dao.RechargeRecordDao;
import cn.ztuo.bitrade.entity.MemberTransaction;
import cn.ztuo.bitrade.entity.RechargeRecord;
import cn.ztuo.bitrade.entity.ReleaseBalance;
import cn.ztuo.bitrade.pagination.Criteria;
import cn.ztuo.bitrade.pagination.Restrictions;
import cn.ztuo.bitrade.service.Base.BaseService;
import com.querydsl.core.types.Predicate;
import org.hibernate.criterion.Expression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class RechargeRecordService extends BaseService<RechargeRecord> {

    @Autowired
    private RechargeRecordDao rechargeRecordDao;
    @Autowired
    private MemberTransactionService transactionService;

    /**
     * 保存
     *
     * @param rechargeRecord
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public RechargeRecord save(RechargeRecord rechargeRecord) {

        MemberTransaction transaction = new MemberTransaction();
        transaction.setAmount(rechargeRecord.getAmount());
        transaction.setSymbol(rechargeRecord.getUnit());
        transaction.setAddress(rechargeRecord.getAddress());
        transaction.setMemberId(rechargeRecord.getMemberId());
        transaction.setType(TransactionType.MANUAL_RECHARGE_RRCORD);
        transaction.setFee(BigDecimal.ZERO);
        transactionService.save(transaction);

        return rechargeRecordDao.save(rechargeRecord);
    }


    /**
     * 查询
     *
     * @param
     * @return
     */
    public Page<RechargeRecord> findAll(PageModel pageModel, RechargeRecord rechargeRecord) {

        // 查询条件
        Criteria<RechargeRecord> criteria = new Criteria<>();

        // 条件查询
        criteria.add(Restrictions.eq("unit", rechargeRecord.getUnit(), true));

        if (!StringUtils.isEmpty(rechargeRecord.getUsername())) {
            criteria.add(Restrictions.or(
                    Restrictions.like("username", rechargeRecord.getUsername(), true),
                    Restrictions.like("mobilePhone", rechargeRecord.getUsername(), true)
            ));
        }

        // 根据注册时间倒序
        Sort sorts = Criteria.sortStatic("id.desc");

        // 分页
        PageRequest pageRequest = new PageRequest(pageModel.getPageNo() - 1, pageModel.getPageSize(), sorts);

        return rechargeRecordDao.findAll(criteria, pageRequest);
    }


    /**
     * 查询
     *
     * @param
     * @return
     */
    public List<RechargeRecord> findAll() {
        return rechargeRecordDao.findAll();
    }

}
